<?php
/**
 * Created by PhpStorm.
 * Script Name: Tpzs.php
 * Create: 2022/4/18 10:38
 * Description: 优惠券助手处理器
 * Author: fudaoji<fdj@kuryun.cn>
 */
namespace app\bot\controller;

use app\admin\model\BotGroupmember;
use app\admin\model\BotMember;
use app\common\model\zdjr\Block;
use app\common\model\zdjr\Clue;
use app\common\model\zdjr\Rule;
use ky\Logger;

class Zdjr extends Addon
{
    private $toWxid = '';

    /**
     * @var Rule
     */
    private $ruleM;
    /**
     * @var Clue
     */
    private $clueM;
    /**
     * @var array|false|\PDOStatement|string|\think\Model
     */
    private $task;
    /**
     * @var array|false|\PDOStatement|string|\think\Model
     */
    private $clue;
    /**
     * @var Block
     */
    private $blockM;

    public function init($options = [])
    {
        parent::init($options); // TODO: Change the autogenerated stub
        $this->ruleM = new Rule();
        $this->clueM = new Clue();
        $this->blockM = new Block();
        $this->memberM = new BotMember();
        $this->groupMemberM = new BotGroupmember();
        $this->task = $this->ruleM->getOneByMap([
            'bots' => ['like', '%'.$this->bot['id'].'i%'], 'status' => 1
        ],true, true);
        return $this;
    }

    public function groupMemberAddHandle(){
        if(empty($this->task)){
            return false;
        }
        $guest = $this->botClient->getGuest($this->content);
        $nickname = $guest['nickname'];
    }

    /**
     * 机器人主动事件
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function deviceCallbackHandle(){
        $this->groupChatHandle();
    }

    /**
     * 群聊处理器
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function groupChatHandle(){
        if(empty($this->task)){
            return false;
        }
    }

    private function blockBot(){
        if(mb_strpos($this->content['msg'], '打招呼频率过高') !== false){
            $this->blockM->blockBot([
                'admin_id' => $this->bot['admin_id'],
                'bots' => $this->task['bots'],
                'bot_id' => $this->bot['id']
            ]);
        }
    }

    /**
     * 私聊处理器
     * Author: fudaoji<fdj@kuryun.cn>
     * @throws \think\db\exception\DbException
     * @throws \think\Exception
     */
    public function privateChatHandle(){
        $this->blockBot();
        if(! $this->isClue()){
            return false;
        }
        $this->toWxid = $this->fromWxid;
        $rules = json_decode($this->task['rules'], true);
        if($this->isNewFriend){
            //修改状态
            $this->clueM->updateOne(['id' => $this->clue['id'], 'step' => Clue::STEP_ADDED, 'wxid' => $this->fromWxid]);

            //备注名称
            if(!empty($rules['remark_name'])){
                $remark_name = str_replace(['[名称]', '[电话]'], [$this->clue['title'], $this->clue['content']], $rules['remark_name']);
                    //str_replace('[名称]', $this->clue['title'], $rules['remark_name']);

                $res = $this->botClient->setFriendRemarkName([
                    'robot_wxid' => $this->botWxid,
                    'to_wxid' => $this->fromWxid,
                    'note' => $remark_name
                ]);
            }
            //update at 09.16
            //发送名片
            if(!empty($rules['send_card'])){
                $this->botClient->sendCardToFriends([
                    'robot_wxid' => $this->botWxid,
                    'to_wxid' => $rules['send_card'],
                    'content' => $this->fromWxid
                ]);
            }
            if(!empty($rules['group_way'])){
                switch ($rules['group_way']){
                    case 1: //自动建群
                        break;
                    default:
                        //自动拉群
                        if(!empty($rules['groups'])){
                            $groups = explode(',', $rules['groups']);
                            foreach ($groups as $group_wxid){
                                $group = $this->memberM->getOneByMap(['wxid' => $group_wxid, 'uin' => $this->botWxid], true, true);
                                if($group){
                                    if($rules['group_person_limit'] && $this->groupMemberM->total(['group_id' => $group['id']], true) >= $rules['group_person_limit']){
                                        continue; //群人数已满
                                    }
                                    if($rules['invite_way'] == Rule::INVITE_LINK){
                                        //Logger::error('链接拉群');
                                        $this->botClient->inviteInGroupByLink([
                                            'robot_wxid' => $this->botWxid,
                                            'group_wxid' => $group_wxid,
                                            'friend_wxid' => $this->fromWxid
                                        ]);
                                    }else{
                                        //Logger::error('直接拉群');
                                        $this->botClient->inviteInGroup([
                                            'robot_wxid' => $this->botWxid,
                                            'group_wxid' => $this->groupWxid,
                                            'friend_wxid' => $this->fromWxid
                                        ]);
                                    }
                                }
                            }
                        }
                        break;
                }
            }
        }else{
            Logger::error($this->content);
        }
    }

    /**
     * 是否是线索
     * @return bool
     * @throws \think\db\exception\DbException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    private function isClue(){
        $flag = ($this->task
            && (
            ($this->clue = $this->clueM->getOneByMap(['admin_id' => $this->bot['admin_id'], 'wxid' => $this->fromWxid], true, true))
                || ($this->clue = $this->clueM->getOneByMap(['admin_id' => $this->bot['admin_id'], 'nickname' => filter_emoji($this->content['from_name'])], true, true))
            )
        );
        //Logger::error($this->clue);
        return $flag;
    }

    /**
     * 关键词回复
     * Author: fudaoji<fdj@kuryun.cn>
     */
    private function keyword(){
        if($this->content['msg'] == 'good'){
            $this->botClient->sendTextToFriends([
                'robot_wxid' => $this->botWxid,
                'to_wxid' => $this->groupWxid,
                'msg' => 'ooooo'
            ]);
        }
    }
}